package com.hyfrogx.modules.manage.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.hyfrogx.common.exception.ServiceException;
import com.hyfrogx.common.param.JSONResult;
import com.hyfrogx.common.qcloud.UploadUtils;
import com.hyfrogx.common.utils.QueryExt;
import com.hyfrogx.common.utils.R;
import com.hyfrogx.common.validator.ValidatorUtils;
import com.hyfrogx.modules.app.utils.FrogConstant;
import com.hyfrogx.modules.manage.entity.smallVideo.HyVideoSmallEntity;
import com.hyfrogx.modules.manage.form.video.HyVideoSmallAddForm;
import com.hyfrogx.modules.manage.form.video.HyVideoSmallBatchUpdateForm;
import com.hyfrogx.modules.manage.form.video.HyVideoSmallUpdateForm;
import com.hyfrogx.modules.manage.service.HyVideoSmallService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

@RestController
@RequestMapping("/manage/smallVideo")
@Api(value = "小视频controller", tags = {"后台-小视频"})
public class HyVideoSmallController extends BaseController {

    @Autowired
    HyVideoSmallService hyVideoSmallService;

    /**
     * 小视频列表
     * @param param
     * @return
     */
    @PostMapping("/list")
    public R list(@RequestBody Map<String,Object> param) {
        return R.ok().put("data",hyVideoSmallService.selectBycondition(new QueryExt<>(param)));
    }




    /**
     * 更新类别排
     * @param info
     * @return
     */
    @PostMapping("/sort")
    public R sort(String info) {
        if (StrUtil.isEmpty(info)) {
            return R.ok();
        }
        JSONArray ja = JSONArray.parseArray(info);
        List<HyVideoSmallEntity> list = new ArrayList<HyVideoSmallEntity>();
        for (int i = 0; i < ja.size(); i++) {
            HyVideoSmallEntity object = ja.getObject(i, HyVideoSmallEntity.class);
            list.add(object);
        }
        try {
            hyVideoSmallService.updateSort(list);
            return R.ok("操作成功");
        } catch (ServiceException e) {
            return R.error(e.getMessage());
        }
    }

    /**
     * 编辑图片上传
     * @return
     */
    @PostMapping("/uploadimg")
    public JSONResult upload(@RequestParam("file") MultipartFile file) {
        String result = null;
        if(file!=null){
            String fileName=UUID.randomUUID().toString() + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."), file.getOriginalFilename().length());
            result=UploadUtils.upload(UploadUtils.convertFile(file),fileName, FrogConstant.Path.WULI);
        }
        return ajaxSuccess(result);
    }


    /**
     * 新增小视频
     * @param
     * @return
     */
    @PostMapping("/create")
    public JSONResult create(@RequestBody HyVideoSmallAddForm form) {
        try {
            ValidatorUtils.validateEntity(form);
            HyVideoSmallEntity target=new HyVideoSmallEntity();
            target.setCreateTime(new Date());
            BeanUtil.copyProperties(form,target);
            hyVideoSmallService.getBaseMapper().insert(target);
            return ajaxSuccess("操作成功");
        } catch (Exception e) {
            return ajaxFail(e.getMessage());
        }
    }

//    /**
//     * 小视频详情
//     */
//    @PostMapping("/detail/{id}")
//    public R detail(@PathVariable Integer id) {
//        HyVideoSmallEntity detail=hyVideoSmallService.getBaseMapper().selectById(id);
//        return R.ok().put("detail",detail);
//    }

    /**
     * 编辑
     */
    @PostMapping("/update")
    public R edit(@RequestBody HyVideoSmallUpdateForm form) {
        try {
            ValidatorUtils.validateEntity(form);
            HyVideoSmallEntity target=new HyVideoSmallEntity();
            BeanUtil.copyProperties(form,target);
            if(target.getState()!=null&&target.getState()==1){
                target.setPublishTime(new Date());
            }
            hyVideoSmallService.getBaseMapper().updateById(target);
            return R.ok("操作成功");
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    /**
     * 删除
     */
    @PostMapping("/delete/{id}")
    public JSONResult deletewuliById(@PathVariable Integer id) {
        try {
            hyVideoSmallService.getBaseMapper().deleteById(id);
            return ajaxSuccess("删除成功");
        } catch (Exception e) {
            return ajaxFail(e.getMessage());
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PostMapping("/batchDel")
    public JSONResult batchEditDel(@RequestBody List ids) {
        try {
            hyVideoSmallService.batchDelete(ids);
            return ajaxSuccess("更新成功");
        } catch (Exception e) {
            return ajaxFail(e.getMessage());
        }
    }

    /**
     * 定时清0每日数据
     */
    @PostMapping("/clearData")
    public JSONResult clearData() {
        try {
            hyVideoSmallService.clearData();
            return ajaxSuccess("操作成功");
        } catch (Exception e) {
            return ajaxFail(e.getMessage());
        }
    }

    /**
     * 批量上架下架
     * @return
     */
    @PostMapping("/batchEdit/state")
    public R batchEditState(@RequestBody HyVideoSmallBatchUpdateForm form) {
        try {
           List<String> idList=form.getIds();
            for (int i = 0; i < idList.size(); i++) {
                hyVideoSmallService.updateState(Integer.parseInt(idList.get(i)),form.getState());
            }
            return R.ok("更新成功");
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }



}
